package br.com.simora.component;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.log4j.Logger;

public class HttpTools {
	private static Logger logger = Logger.getLogger(HttpTools.class);
    
    public String sendPostJson(String urlDest, String json){
        String result = "";
        try {
        	URL url = new URL(urlDest);
            HttpPost httpPost = new HttpPost(urlDest);
            StringEntity entity = new StringEntity(json);
            entity.setContentType("application/json");
            HttpURLConnection conn;
            httpPost.setEntity(entity);
            String line;
            BufferedReader rd;
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30 * 1000).setSocketTimeout(30000).build();
            HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
            HttpResponse response = client.execute(httpPost);
             conn = (HttpURLConnection) url.openConnection();
             conn.setRequestMethod("GET");
             rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
             while ((line = rd.readLine()) != null) {
                result += line;
             }
             rd.close();
          } catch (Exception e) {
             e.printStackTrace();
          }
        logger.debug("TESTE DE " + urlDest);
        logger.debug("JSON ENVIO: " + json);
        logger.debug("JSON RETORNO: " + result);
        logger.debug("TESTE DE " + urlDest);
        return result;
    }
	
}